import store from './../store';


let getMpMenuButtonBoundingLeft = () => {
	// #ifndef MP
	return 0;
	// #endif
	return store.getters['ba/getMpMenuButtonBoundingLeft'];
};

let dwidth = () => {
	// #ifdef H5
	if (window && window.DPWidth) return window.DPWidth;
	// #endif

	return uni.getSystemInfoSync().screenWidth;
}

let _dheight = -1;
let dheight = () => {
	//if (_dheight > -1) return _dheight;

	_dheight = uni.getSystemInfoSync().screenHeight;
	return _dheight;
}

let isSinglePageMode = () => {
	return getApp().globalData.singlePageMode;
};

let safeAreaTop = () => {
	return store.getters['ba/getSafeAreaTop'];
};

let safeAreaBottom = () => {
	let systemInfo = uni.getSystemInfoSync();
	if (!systemInfo.safeArea || !systemInfo.safeArea.bottom) return 0;
	return systemInfo.screenHeight - systemInfo.safeArea.bottom;
};


//我们设置导航栏的高度为100rpx
let navHeight = () => {
	return 50;
};

//px转rpx
let rpx2px = (rpx) => {
	return dwidth() / 750 * rpx;
};



function h5GetTextWidth(text, f) {
	const dom = document.createElement('span');
	dom.style.cssText = 'display:inline-block;position: absolute;top:-200px;white-space:nowrap;' + (f ? f : '');
	dom.innerHTML = text;
	document.body.appendChild(dom);
	const width = dom.clientWidth;
	document.body.removeChild(dom);
	return width;
}

//获取小程序root font size
let getXCXRootFontSize = () => {
	let globalData = getApp().globalData;
	if (!globalData.xcxRootFontSize) globalData.xcxRootFontSize = uni.getSystemInfoSync().windowWidth / 7.5 + 'px';

	return globalData.xcxRootFontSize;
}

//获取feed Item 节点高度
let getFeedItemHeight = item => {
	let height = 0;
	//1. 图片高度，按照 750 的比例计算高度
	if (item.obicon && item.obicon.height) height += 750 / item.obicon.width * item.obicon.height;

	if (item.title) height += 60;
	if (item.title && item.title.length > 12) height += 60;

	if (item.stitle) height += 60;
	if (item.utitle) height += 40;
	if (item.tags && item.tags.length > 0) height += 40;
	if (item.obprice && item.obprice.shprice) height += 40;
	if (item.sinfo || (item.user && item.user.name)) height += 80;
	if (item.more && item.more.title) height += 80;

	return height;
};

let numFx = num => {
	if (!num) return '0';
	num = num * 1;
	if (num > 10000) {
		num = (num / 10000).toFixed(2);
		if (num >= 10) num = (num * 1).toFixed(1);
		if (num >= 100) num = (num * 1).toFixed(0);

		num += 'w';
	}

	return num;
};


let shWH = (width, height, pwidth) => {
	let rs = {};
	rs.width = 0;
	rs.height = 0;

	if (!pwidth) pwidth = dwidth() - 30;
	if (!width * 1 || !height * 1) return rs;

	width = width * 1;
	height = height * 1;
	if (height > width) {
		rs.height = pwidth;
		rs.width = rs.height / height * width;

		return rs;
	}

	rs.width = pwidth;
	rs.height = rs.width / width * height;
	return rs;
};

module.exports = {
	dwidth,
	dheight,

	getMpMenuButtonBoundingLeft,

	navHeight,
	rpx2px,

	h5GetTextWidth,
	getXCXRootFontSize,
	getFeedItemHeight,
	numFx,
	shWH,

	safeAreaTop,
	safeAreaBottom,

	isSinglePageMode,
}
